// import parseTime, formatTime and set to filter
export { parseTime, formatTime } from '@/utils'

/**
 * Show plural label if time is plural number
 * @param {number} time
 * @param {string} label
 * @return {string}
 */
function pluralize(time, label) {
  if (time === 1) {
    return time + label
  }
  return time + label + 's'
}

/**
 * @param {number} time
 */
export function timeAgo(time) {
  const between = Date.now() / 1000 - Number(time)
  if (between < 3600) {
    return pluralize(~~(between / 60), ' minute')
  } else if (between < 86400) {
    return pluralize(~~(between / 3600), ' hour')
  } else {
    return pluralize(~~(between / 86400), ' day')
  }
}

/**
 * Number formatting
 * like 10000 => 10k
 * @param {number} num
 * @param {number} digits
 */
export function numberFormatter(num, digits) {
  const si = [
    { value: 1e18, symbol: 'E' },
    { value: 1e15, symbol: 'P' },
    { value: 1e12, symbol: 'T' },
    { value: 1e9, symbol: 'G' },
    { value: 1e6, symbol: 'M' },
    { value: 1e3, symbol: 'k' }
  ]
  for (let i = 0; i < si.length; i++) {
    if (num >= si[i].value) {
      return (
        (num / si[i].value)
          .toFixed(digits)
          .replace(/\.0+$|(\.[0-9]*[1-9])0+$/, '$1') + si[i].symbol
      )
    }
  }
  return num.toString()
}

/**
 * 10000 => "10,000"
 * @param {number} num
 */
export function toThousandFilter(num) {
  if (num === null) {
    return '-'
  } else {
    var values = (+num || 0).toString().split('.')
    return `￥${values[0]
      .toString()
      .replace(/^-?\d+/g, (m) => m.replace(/(?=(?!\b)(\d{3})+$)/g, ','))}${
      values[1] === undefined ? '' : '.' + values[1]
    }`
  }
}

/**
 * Upper case first char
 * @param {String} string
 */
export function uppercaseFirst(string) {
  return string.charAt(0).toUpperCase() + string.slice(1)
}

/**
 * 文件类型转义
 */
export function fileTypeFilter(number) {
  switch (number) {
    case -1:
      return '文件夹'
    case 0:
      return '文档'
    case 1:
      return '视频'
  }
}

/**
 * 空值占位符
 */
export function nullPlaceholder(str) {
  if (str) return str 
  else return '-'
}

/**
 * 性别转义
 */
export function sex(val) {
  switch (val) {
    case 0:
      return '男'
    case 1:
      return '女'
    default:
      return '-'
  }
}

/**
 * 权限转义
 */
export function isAdmin(val) {
  switch (val) {
    case 0:
      return '普通员工'
    case 1:
      return '部门管理员'
  }
}

/**
 * 格式化UTC时间
 * @param {string} val
 * @param {boolean} flag
 * @returns {string}
 */
export function formatUtcTime(val) {
  if(!val || val === 0 || val === '0001-01-01T00:00:00') return '';
  const index = val.indexOf('T')
  if(index < 0) {
      return val
  } else {
    const date = val.substr(0, index)
    const time = val.substr(index + 1, val.length)
    const arr = time.split(':')
    for (let i = 0; i < arr.length; i++) {
        const value = parseInt(arr[i]).toString()
        if (value.length < 2) {
            arr[i] = '0' + parseInt(arr[i]).toString()
        } else {
            arr[i] = parseInt(arr[i]).toString()
        }
    }
    return date + ' ' + arr.join(':')
  }
}
